#include <myheader_c_plus.h>
#include <cmath>
#include <set>
using std::multiset;
using std::ostream;
using std::set;
using std::cout;
using std::endl;
class Point{
public:
    Point(int x, int y)
        :_x(x)
         ,_y(y)
    {

    }

    float getDistance()const{
        return hypot(_x, _y); 
    }

    int getX()const{
        return _x;
    }

    int getY()const{
        return _y;
    }




    friend ostream & operator<<(ostream & os, const Point & rhs);
private:
    int _x;
    int _y;
};
ostream & operator<<(ostream & os, const Point & rhs){
    os <<"(" << rhs._x << "," << rhs._y << ")" ;
    return os;

}
//第一种方式
bool operator<(const Point & lhs, const Point & rhs){
    cout << "operator < " << endl;
    float l = lhs.getDistance();
    float r = rhs.getDistance();
    if(l != r) return l < r;
    if(lhs.getX() != rhs.getX()) return lhs.getX() < rhs.getX();
    return lhs.getY() < rhs.getY();

}

//第二种方式模板化特例
namespace std{
template<>
    struct less<Point>{
        bool operator()(const Point &lhs, const Point &rhs) const 
        {
            cout << "struct less<Point>" << endl;
            float l = lhs.getDistance();
            float r = rhs.getDistance();
            if(l != r) return l < r;
            if(lhs.getX() != rhs.getX()) return lhs.getX() < rhs.getX();
            return lhs.getY() < rhs.getY();

        }
    };


};//end of namespace std

//函数对象方式
struct ComparePoint{
    bool operator()(const Point & lhs, const Point & rhs)const{
        cout << "ComparePoint" << endl;
        float l = lhs.getDistance();
        float r = rhs.getDistance();
        if(l != r) return l < r;
        if(lhs.getX() != rhs.getX()) return lhs.getX() < rhs.getX();
        return lhs.getY() < rhs.getY();
    }
};

template<typename Container>
void display(Container & con){
    for(auto & ele : con){
        cout << ele << " ";
    }
    cout << endl;
}
void test(){
    set<Point, ComparePoint> points = {
        Point(1,3),
        Point(-1,3),
        Point(0,3),
        Point(2,3),
        Point(1,3),
        Point(3,1)

    };
    display(points);
}

void test2(){
    set<int> num = {1,2,2,3,4,5};
    cout << num.count(2);
    auto it = num.find(0);
    if(it != num.end()){
        cout << "no this number" << endl;
    }

}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

